package com.ruoyi.qywx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.qywx.entity.ContactWayGroup;
import com.ruoyi.qywx.mapper.ContactWayGroupMapper;
import com.ruoyi.qywx.model.vo.ContactWayGroupVo;
import com.ruoyi.qywx.service.inters.ContactWayGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : hcl
 * @Date : 2023/8/14 16:18
 */
@Service
public class ContactWayGroupServiceImpl implements ContactWayGroupService {
    @Resource
    private ContactWayGroupMapper wayGroupMapper;

    @Transactional
    @Override
    public AjaxResult addContactWayGroup(ContactWayGroupVo vo, Long teamId) {
        if (NumberUtils.isTrue(vo.getId())) {
            List<ContactWayGroup> wayGroups = wayGroupMapper.selectList(new QueryWrapper<ContactWayGroup>()
                    .eq("team_id", teamId)
                    .eq("group_name", vo.getName())
                    .ne("id", vo.getId())
            );
            if (CollectionUtils.isNotEmpty(wayGroups)) {
                return AjaxResult.error("分组名称不能重复");
            }
            wayGroupMapper.updateById(ContactWayGroup.builder()
                    .id(vo.getId())
                    .groupName(vo.getName())
                    .build());
        } else {
            ContactWayGroup wayGroups = wayGroupMapper.selectOne(new QueryWrapper<ContactWayGroup>()
                    .eq("team_id", teamId)
                    .eq("group_name", vo.getName()));
            if (wayGroups != null) {
                return AjaxResult.error("分组名称不能重复");
            }
            wayGroupMapper.insert(ContactWayGroup.builder()
                    .groupName(vo.getName())
                    .teamId(teamId)
                    .build());
        }
        return AjaxResult.success();
    }

    @Transactional
    @Override
    public AjaxResult delContactWayGroup(Long groupId, Long teamId) {
        QueryWrapper<ContactWayGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("id", groupId).eq("team_id", teamId);
        ContactWayGroup wayGroup = wayGroupMapper.selectOne(wrapper);
        if (wayGroup == null) {
            return AjaxResult.error("分组不存在");
        }
        wayGroupMapper.deleteById(groupId);
        return AjaxResult.success();
    }

    @Override
    public List<ContactWayGroupVo> contactWayGroupList(Long teamId) {
        List<ContactWayGroup> wayGroups = wayGroupMapper.selectList(new QueryWrapper<ContactWayGroup>()
                .eq("team_id", teamId));
        if (CollectionUtils.isEmpty(wayGroups)) {
            return Collections.emptyList();
        }
        return wayGroups.stream().map(g -> new ContactWayGroupVo(g.getId(), g.getGroupName()))
                .collect(Collectors.toList());
    }
}
